package com.repu.dyz.controller;

import com.repu.dyz.exception.BusinessException;
import com.repu.dyz.exception.code.BaseResponseCode;
import com.repu.dyz.utils.DataResult;
import com.repu.dyz.vo.TestReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassNametestController
 * @Description
 * @Author: daiyizheng
 * @Date2020/12/9 11:11 下午
 * @Version V1.0
 **/
@RestController
@RequestMapping("/swagger")
@Api(tags = "测试接口模块", description = "主要测试使用")
public class TestController {

    @GetMapping("/test")
    @ApiOperation(value = "测试接口")
    public String test(){

        return "测试接口";
    }

    @GetMapping("/test/data")
    @ApiOperation(value = "统一的响应格式")
    public DataResult<String> test1(){
        return new DataResult<>();
    }

    @GetMapping("/home")
    @ApiOperation(value = "测试全局异常接口")
    public  DataResult<String> getHome(){
        int i= 1/0;
        DataResult<String> result = DataResult.success("测试成功");

        return result;
    }

    @GetMapping("/business/error")
    @ApiOperation(value = "测试主动抛出业务异常接口")
    public DataResult<String> testBusinessError(@RequestParam String type){
        if(!(type.equals("1")||type.equals("2")||type.equals("3"))){
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
    }
        DataResult<String> result=new DataResult(0,type);
        return result;
    }

    @PostMapping("/valid/error")
    @ApiOperation(value = "测试Validator抛出业务异常接口")
    public DataResult testValid(@RequestBody @Valid TestReqVO vo){
        DataResult result=DataResult.success();
        return result;
    }
}
